package com.gitee.chessboard.jdbc.parser;

import com.gitee.chessboard.jdbc.JdbcManager;

import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 将对象转换成insertOrUpdate的sql
 *
 * @author 敲代码的旺财
 * @date 2021/11/25 13:37
 */
public class InsertOrUpdateParser extends InsertParser {


    public InsertOrUpdateParser(JdbcManager jdbcManager) {
        super(jdbcManager);
    }

    /**
     * 将对象转成创建表的sql
     *
     * @param obj 需要解析的对象
     * @return 建表sql语句
     */
    @Override
    public String convert(Object obj) {
        ParserHandler parserHandler = jdbcManager.getParserHandler(obj);
        String tableId = parserHandler.getTableId();
        String fieldName = parserHandler.getTableFieldNameAndFieldName().get(tableId);
        String insertSql = super.convert(obj);
        StringBuilder sb = new StringBuilder(insertSql);
        sb.append(" on duplicate key update");
        sb.append(" "+tableId+" = #{"+fieldName+"}");
        return sb.toString();
    }

}
